;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Corruption.DeliveryNoteWarningDbd", [])
        .controller("DeliveryNoteWarningDbdCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService","$stateParams", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService,$stateParams) {
            $scope.markInfo = {
                allChecked: false,
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.toolbarConfig = {
                showColConfig: false
            };


            $scope.condition = {};
            $scope.dataList = [];
            $scope.tbConf = {
                scrollWidth: 1070,
                tableCol: [
                    {
                        type: "",
                        name: "销售单号",
                        dataField: "saleNo",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "发货通知单号",
                        dataField: "deliveryNo",
                        width: 170,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "零件号",
                        dataField: "materialsNo",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "零件中文名",
                        dataField: "materialsDes",
                        width: 280,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "制单时间",
                        dataField: "createDateShow",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "客户名称",
                        dataField: "purchaseOrgName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "出库点",
                        dataField: "outArea",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "SAP销售订单",
                        dataField: "sapSaleOrder",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "SAP外向发货单",
                        dataField: "sapOutBill",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "零件凭证号",
                        dataField: "materialsBillNo",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "价格组",
                        dataField: "priceSchemeName",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "本次发货数量",
                        dataField: "deliveryQty",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    }, {
                        type: "",
                        name: "已出库数量",
                        dataField: "inventoryOutAmount",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    }, {
                        type: "",
                        name: "单据数量",
                        dataField: "amountAfterAdjust",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    }, {
                        type: "",
                        name: "已创建发货通知数",
                        dataField: "sendQty",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    }, {
                        type: "",
                        name: "是否开票",
                        dataField: "tickerStateShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "是否过账",
                        dataField: "isPostingShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        name: "是否直投",
                        dataField: "isDirectShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "出库状态",
                        dataField: "statusShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    }, {
                        type: "",
                        name: "制单人",
                        dataField: "createName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    }, {
                        type: "",
                        name: "单据状态",
                        dataField: "dbStatusShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
						name: "督办情况",
						dataField: "dbRemark",
						width: 140,
						isShow: true,
						isFixed: false,
					}
                    , {
                        type: "",
                        name: "备注",
                        dataField: "remark",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };

            $scope.exportParam = {
                sectionParam: {ids: []},
                allUrl: ybHttp.RequestUrl("device", "/warnNote/dbdExportAll")
            };

            var btnPermissionCode = {
                deliveryNoteWarningDbdCtrlEdit: window.ActionCode.deliveryNoteWarningDbdCtrlEdit,
                deliveryNoteWarningDbdCtrlExportAll: window.ActionCode.deliveryNoteWarningDbdCtrlExportAll,
                deliveryNoteWarningDbdCtrlWanCheng: window.ActionCode.deliveryNoteWarningDbdCtrlWanCheng,
            };

            $scope.btnPermission = {
                deliveryNoteWarningDbdCtrlEdit:true,
                deliveryNoteWarningDbdCtrlExportAll: true,
                deliveryNoteWarningDbdCtrlWanCheng: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }
            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            function getDataList(curPage) {
                $scope.markInfo.allChecked = false;
                $scope.markInfo.curRow = -1;
                $scope.exportParam.sectionParam.ids = [];
                var service = "device";
                var reqUrl = "/warnNote/deliveryNoteWarnListDb";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.createDateShow = $filter("YbDate")(item.createDate, "yyyy-MM-dd HH:mm:ss");
                        item.statusShow = $filter("ProgressStatus")(item.status, "outDepot");
                        item.tickerStateShow = $filter("TrueOrFalseOrNull")(item.tickerState);
                        item.isPostingShow = $filter("TrueOrFalseOrNull")(item.isPosting);
                        item.isDirectShow = $filter("TrueOrFalseOrNull")(item.isDirect);
                        item.warnNoteStatusShow = $filter("ProgressStatus")(item.warnNoteStatus, "warnNoteStatus");
                        item.dbFlagShow = $filter("ProgressStatus")(item.dbFlag, "dbFlag");
                        item.dbStatusShow = $filter("ProgressStatus")(item.dbStatus, "priceChangeDB");
                        item.isChecked = false;
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };
			$scope.updateDbRemark = function(item) {
				handleUpdateDbRemark([item.noteDetailId]);
			};
			$scope.updateDbRemarkBatch = function() {
				const selectedDatas = $scope.dataList.filter(df => df.isChecked);
                if (selectedDatas.length === 0) {
                    ybWidget.ShowMessage('请选择需要操作的行');
                    return;
                }
				handleUpdateDbRemark(selectedDatas.map(s => s.noteDetailId));
			};

            function handleUpdateDbRemark(ids) {
				ybWidget.ShowConfirm("确定督办？", '督办情况').then(function (val) {
					var service = "device";
					var reqUrl = "/warnNote/updateDb";

                    var param = {
						ids: ids,
						dbRemark: val,
                    };
                    ybHttp.Post(service, reqUrl, param).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    })
                });
			}

            $scope.createdThread = function() {
				const selectedDatas = $scope.dataList.filter(df => df.isChecked);
                if (selectedDatas.length === 0) {
                    ybWidget.ShowMessage('请选择需要操作的行');
                    return;
                }

                ybWidget.ShowMessage("确定要生成线索吗？", MESG_TYPE.Ask).then(function () {
                    var service = "device";
                    var reqUrl = "/clew/add";
					let params = {
						source:"仓储物流",
						type:"发货通知督办单",
						billNoList:selectedDatas.map(s => s.noteDetailId)
					}
                    ybHttp.Post(service, reqUrl, params).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    });
                })
			}

            $scope.addWarning = function () {
                const selectedDatas = $scope.dataList.filter(df => df.isChecked);
                if (selectedDatas.length === 0) {
                    ybWidget.ShowMessage('请选择需要操作的行');
                    return;
                }

                ybWidget.ShowMessage("确定要生成督办单吗？", MESG_TYPE.Ask).then(function () {
                    var service = "device";
                    var reqUrl = "/warnNote/generateDb";
                    ybHttp.Post(service, reqUrl, {ids:selectedDatas.map(s => s.noteDetailId)}).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    });
                })
            };

            function handleUpdateRemark(ids) {
                ybWidget.ShowConfirm("维护备注", [{label: '备注'}]).then(function (data) {
                    var service = "device";
                    var reqUrl = "/warnNote/editWarnNote";

                    var param = {
                        ids: ids,
                        remark: data[0].value,
                    };
                    ybHttp.Post(service, reqUrl, param).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    })
                });
            }


            $scope.updateRemarkBatch = function () {
                const selectedDatas = $scope.dataList.filter(df => df.isChecked);
                if (selectedDatas.length === 0) {
                    ybWidget.ShowMessage('请选择需要操作的行');
                    return;
                }
                handleUpdateRemark(selectedDatas.map(s => s.noteDetailId));
            };


            $scope.updateRemark = function (item) {
                handleUpdateRemark([item.noteDetailId]);
            };

            function getBillIds() {
                $scope.exportParam.sectionParam.ids = [];
                angular.forEach($scope.dataList, function (item) {
                    if (!!item.isChecked) {
                        $scope.exportParam.sectionParam.ids.push(item.id);
                    }
                });
            }

            $scope.selectAll = function (list, type) {
                for (var i = 0, len = list.length; i < len; i++) {
                    list[i].isChecked = angular.isUndefined(type) ? $scope.markInfo.allChecked : $scope.markInfo["allChecked" + type];
                }
                getBillIds();
            };

            function checkAll(list) {
                var selectAll = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.selectSingle = function (item, list, type) {
                var selectAll = false;
                if (!item.isChecked) {
                    selectAll = false;
                } else {
                    if (!checkAll(list)) {
                        selectAll = false;
                    } else {
                        selectAll = true;
                    }
                }
                if (angular.isDefined(type)) {
                    $scope.markInfo["allChecked" + type] = selectAll;
                } else {
                    $scope.markInfo.allChecked = selectAll;
                }
                getBillIds();
            };


            (function () {
                if ($stateParams.billNo) {
					$scope.condition.noteDetailId = $stateParams.billNo
				}
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);